package com.hafu.dao;

import com.hafu.model.GysInfo;
import com.hafu.util.DbUtil;
import com.hafu.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GysInfoDao {
    public List<GysInfo> getGysInfoByName(String name) {
        Connection conn = DbUtil.getConn();
        if (conn == null) {
            throw new RuntimeException("数据库连接失败");
        }
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<GysInfo> gysInfos = new ArrayList<>();
        try {
            String sql = "SELECT * FROM gysinfo WHERE name = ?";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, name);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                GysInfo gysInfo = new GysInfo();
                gysInfo.setId(resultSet.getInt("id"));
                gysInfo.setName(resultSet.getString("name"));
                gysInfo.setTel(resultSet.getString("tel"));
                gysInfos.add(gysInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return gysInfos;
    }

    public boolean addGysInfo(GysInfo gysInfo) {
        Connection conn = DbUtil.getConn();
        if (conn == null) {
            throw new RuntimeException("数据库连接失败");
        }
        PreparedStatement preparedStatement = null;
        try {
            String sql = "INSERT INTO gysinfo (name, tel) VALUES (?, ?)";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, gysInfo.getName());
            preparedStatement.setString(2, gysInfo.getTel());
            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (preparedStatement != null) preparedStatement.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


        //删除
        public Boolean deleteGysInfo(int id) {
            String sql = "delete from gysinfo where id=?";
            Connection conn = DbUtil.getConn();
            try {
                PreparedStatement preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setInt(1, id);
                int i = preparedStatement.executeUpdate();
                if (i > 0) {
                    System.out.println("删除成功");
                    return true;
                } else {
                    return false;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return false;
        }
         //修改
            public Boolean updateGysInfo(GysInfo gys){
                String sql="update gysinfo set name=?,tel=? where id=?";
                Connection conn=DbUtil.getConn();
                try {
                    PreparedStatement preparedStatement=conn.prepareStatement(sql);
                    preparedStatement.setString(1,gys.getName());
                    preparedStatement.setString(2,gys.getTel());
                    preparedStatement.setInt(3,gys.getId());
                    int i=preparedStatement.executeUpdate();
                    if (i>0){
                        System.out.println("修改成功");
                        return true;
                    }else return false;
                    }catch (SQLException throwables){
                    throwables.printStackTrace();
                }
                return false;
            }
            //查询
            public ArrayList<GysInfo> getGysInfoByName(GysInfo gys){
                ArrayList<GysInfo> list = new ArrayList<>();
                String sql="select * from gysinfo";
                if (!StringUtil.isEmpty(gys.getName())){
                    sql=sql+" where name like '%"+gys.getName()+"%'";
                }
                Connection conn=DbUtil.getConn();
                try {
                    PreparedStatement preparedStatement=conn.prepareStatement(sql);
                    ResultSet result=preparedStatement.executeQuery();
                    while (result.next()) {
                        int id = result.getInt("id");
                        String name = result.getString("name");
                        String tel = result.getString("tel");
                        GysInfo gysInfo = new GysInfo(id, name, tel);
                        list.add(gysInfo);
                    }
                }catch (SQLException throwables){
                    throwables.printStackTrace();
                }
                return list;
            }
    }

